package com.hospital.hospitalsystem.modules.frontDesk.biz;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hospital.hospitalsystem.base.PageData;
import com.hospital.hospitalsystem.modules.frontDesk.dto.DeleteOrderReq;
import com.hospital.hospitalsystem.modules.frontDesk.dto.SearchOrderListRsp;
import com.hospital.hospitalsystem.modules.frontDesk.dto.SearchOrderReq;
import com.hospital.hospitalsystem.modules.login.dto.EmailEntityExt;
import com.hospital.hospitalsystem.modules.user.biz.UserBiz;
import com.hospital.hospitalsystem.modules.user.dao.entity.BookEntity;
import com.hospital.hospitalsystem.modules.user.dto.OrderReq;
import com.hospital.hospitalsystem.modules.user.service.BookService;
import com.hospital.hospitalsystem.utils.SendEmailUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author qiancheng@cqxyy.net
 * @date 2023/3/30
 */
@Service
public class FrontDeskBiz {

    @Autowired
    private SendEmailUtils sendEmailUtils;

    @Autowired
    private BookService bookService;

    @Autowired
    private UserBiz userBiz;

    /**
     * 前台获取预约信息
     * @param req
     * @return
     */
    public PageData<SearchOrderListRsp> getBook(SearchOrderReq req) {
        Page<BookEntity> page = PageHelper.startPage(
                req.getCurrentPage(), req.getPageSize()
        );
        List<SearchOrderListRsp> list = bookService.getBook(req);
        int longs = ((Number) page.getTotal()).intValue();
        PageData<SearchOrderListRsp> pageData = PageData.of(list,page.getPageNum(),page.getPageSize(),longs);
        return pageData;
    }

    /**
     * 前台更新预约信息
     * @param req
     */
    public void updateBook(SearchOrderReq req) {
        Example exampleBook = new Example(BookEntity.class);
        Example.Criteria criteriaBook = exampleBook.createCriteria();
        criteriaBook.andEqualTo("uuid",req.getUuid());
        criteriaBook.andEqualTo("hospitalUuid",req.getHospitalUuid());
        BookEntity bookEntity = new BookEntity();
        bookEntity.setStatus(2);
        bookService.updateByExampleSelective(bookEntity,exampleBook);
    }

    /**
     * 前台删除预约信息
     * @param req
     */
    public void deleteBook(DeleteOrderReq req) {
        SearchOrderReq searchOrderReq = new SearchOrderReq();
        BeanUtils.copyProperties(req,searchOrderReq);
        SearchOrderListRsp result = bookService.getBook(searchOrderReq).get(0);
        SimpleDateFormat dfDate = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat dfTime = new SimpleDateFormat("HH:mm:ss");
        EmailEntityExt emailEntityExt = new EmailEntityExt();
        emailEntityExt.setData("尊敬的" + result.getUserName() + "用户你好,您" + result.getHospitalName() + "医院下的" + result.getDoctorName() +
                "医生的" + dfDate.format(result.getDay()) + " " + dfTime.format(result.getStartTime()) + "-" + dfTime.format(result.getEndTime()) + "时间的预约被取消\n" +
                "退回原因:" + req.getDeleteMessage());
        emailEntityExt.setEmail(result.getUserEmail());
        emailEntityExt.setSubject(result.getHospitalName() + "医院通知");
        sendEmailUtils.sendEmails(emailEntityExt);
        OrderReq orderReq = new OrderReq();
        orderReq.setUserUuid(result.getUserUuid());
        orderReq.setOrderUuid(req.getUuid());
        orderReq.setDetailUuid(result.getDetailUuid());
        orderReq.setDoctorUuid(result.getDoctorUuid());
        orderReq.setHospitalUuid(req.getHospitalUuid());
        //不变更排号
        orderReq.setIsLevelChange(2);
        userBiz.doctorOrderDelete(orderReq);
    }
}
